//
//  HMPictureView.swift
//  weibo28
//
//  Created by 范英强 on 16/3/26.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
// cell标示
private let cellIdentifier = "cellIdentifier"
// cell间距
private let itemMargin:CGFloat = 5
// cell 的宽高
private let cellWH = (SCREENW - HMStatusCellMargin * 2 - itemMargin * 2) / 3

class HMPictureView: UICollectionView {
    
    var pictureInfo: [HMPictureInfo]?{
        didSet{
            countLable.text = "\(pictureInfo?.count ?? 0)"
            let size = getPictureViewOfSize(pictureInfo?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
               make.size.equalTo(size)
            }
            // 记得刷新数据
            reloadData()
        }
    }
    // 通过图片多少来计算配图的size
    func getPictureViewOfSize(count: Int) -> CGSize{
        
        
        // 计算 列数 和行数
        
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        
        let row =  (count - 1) / 3 + 1
        
        // 计算出配图的宽高
        let w = cellWH * CGFloat(col) + itemMargin * CGFloat(col - 1)
        let h = cellWH * CGFloat(row) + itemMargin * CGFloat(row - 1)
        
        return CGSize(width: w, height: h)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.clearColor()
        // 注册cell
        registerClass(HMPictureViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
        dataSource = self
        //获取layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        // 设置间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        // 01-添加控件
        addSubview(countLable)
        // 02-添加约束
        countLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }

    // MARK: - 懒加载控件
    private lazy var  countLable = UILabel(textColor: UIColor.redColor(), font: 30)
}

extension HMPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! HMPictureViewCell
        cell.picUrl = pictureInfo![indexPath.row]
        return cell
    }
}

class HMPictureViewCell: UICollectionViewCell {
    
    var picUrl: HMPictureInfo?{
        didSet{
            guard let  pic = picUrl?.thumbnail_pic else{
                return
            }
            imageView.sd_setImageWithURL(NSURL(string: pic), placeholderImage: nil)
            
            if pic.hasSuffix(".gif"){
                gitImageView.hidden = false
            }else{
                gitImageView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = themeColor
        
        contentView.addSubview(imageView)
        contentView.addSubview(gitImageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gitImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(imageView)
            make.trailing.equalTo(imageView)
        }
    }
    
    // MARK: - 栏记载控件
    private lazy var  imageView: UIImageView = {
        let img = UIImageView()
        img.contentMode = UIViewContentMode.ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    
    private lazy var  gitImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
}
